package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    //查询列表
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo",departmentService.query(qo));
        return "/department/list";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        departmentService.delete(id);
        return new JsonResult();
    }

    /*//添加和修改跳转页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        if (id != null) {
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "/department/input";
    }*/

    //添加和修改
    /*@RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            }else{
                //添加
                departmentService.save(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"失败");
        }
    }*/
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
        if (department.getId() != null) {
            departmentService.update(department);
        }else{
            //添加
            departmentService.save(department);
        }
        return new JsonResult();
    }
}
